package cn.yunhe.controller;

import cn.yunhe.entity.ExamPaper;
import cn.yunhe.entity.Field;
import cn.yunhe.entity.Point;
import cn.yunhe.entity.User;
import cn.yunhe.service.ExamPaperService;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

@RestController
@RequestMapping("/ExamPage")
public class ExamPaperController {

    @Resource
    private ExamPaperService examPaperService;

    /**
     * 根据知识点分类分页模糊查询试卷
     * @param pageSize 页大小
     * @param pageNum   当前页
     * @param examPaper 考试相关信息
     * @return
     * @author guohanyu
     */
    @RequestMapping("/getExamPaperList")
    public Map queryUserByPage(int pageSize, int pageNum, ExamPaper examPaper,Integer fieldId,Integer pointId) {
        Map map = new HashMap();
        map.put("pageSize", pageSize);
        map.put("pageNum", pageNum);
        //封装数据
        map.put("fieldId",fieldId);
        map.put("pointId",pointId);
        map.put("examPaper", examPaper);
        Page page = examPaperService.getPapersByFieldAndPointAndName(map);
        map.put("page", page);
        map.put("totalPage", page.getPages());

        return map;

    }

    /**
     * 添加试卷
     * @param contentKnowledgePoint 添加的知识点
     * @param examPaperName 试卷名称
     * @param passPoint 及格分
     * @param duration 考试时间
     * @param questionCount 多少试题
     * @param questionPoint 每道多少分
     * @param totalPoint    总分
     * @param field  所属科目
     * @param pointId   所属分类
     * @return
     */
    @RequestMapping("/addExamPaper")
    public ModelAndView addExamPaper(HttpSession session, Integer examPaperType, String contentKnowledgePoint, String examPaperName, Integer passPoint, Integer duration, Integer questionCount, Integer questionPoint, Integer totalPoint, Integer field, Integer pointId ){
        //创建Map封装数据
        Map<String,Object> map = new HashMap<String, Object>();
        //创建ExamPaper对象封装数据
        ExamPaper examPaper = new ExamPaper();
        examPaper.setName(examPaperName);
        examPaper.setPassPoint(passPoint);
        examPaper.setDuration(duration);
        examPaper.setTotalPoint(totalPoint);
        examPaper.setField(new Field(field));
        examPaper.setPoint(new Point(pointId));
        examPaper.setPaperType(examPaperType);
        examPaper.setCreateTime(new Date());
     /*   User user = (User)session.getAttribute("Curr_User");
        examPaper.setCreator(user.getUserName());*/
        examPaper.setStatus(0);

        //知识点数组数据处理
            List<String> pointListString = Arrays.asList(contentKnowledgePoint.split(","));
            List<Integer> pointList = new ArrayList<>();

            for (String pl : pointListString) {
                pointList.add(Integer.parseInt(pl));
                System.out.println(pl);
            }

        //封装入map传入后台
        map.put("examPaper",examPaper);
        map.put("questionCount",questionCount);
        map.put("questionPoint",questionPoint);
        map.put("pointList",pointList);

        //执行新增操作
        examPaperService.addExamPaper(map);




        return new ModelAndView();
    }













    public ExamPaperService getExamPaperService() {
        return examPaperService;
    }

    public void setExamPaperService(ExamPaperService examPaperService) {
        this.examPaperService = examPaperService;
    }
}